<?php

namespace app\admin\controller;

use app\BaseController;
use think\facade\Config;
use think\facade\Cache;
use app\admin\serivce\JwtAuth;


class Index extends BaseController
{

    public function index()
    {
        return \think\facade\App::version();
        // return '<style>*{ padding: 0; margin: 0; }</style><iframe src="https://www.thinkphp.cn/welcome?version=' . \think\facade\App::version() . '" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>';
    }

    public function hello($name = 'ThinkPHP8')
    {
        return 'hello,' . $name;
    }


    public function getSetting()
    {
        var_dump(Config::get('custom.jwt_key'));
    }

    public function testPoint()
    {
        $sum=0;
        for($i=1;$i<=100;$i++){
            $sum +=$i;
        }
        return $sum;
    }

    /**
     * 把明文加密为字符串
     */
    public function getJwtSecret():string
    {
        $arr=[
            // jwt签发者
            'iss'=>'tp8.xxx',
            // jwt接收的一方
            'aud'=>'front.tp8.xxx',
            'exp'=>time()+3600*2,
            // jwt签发时间
            'iat'=>time(),
            // jwt生效时刻，早于此刻不生效
            'nbf'=>time(),
            'data'=>[
                'user_id'=>123,
                'username'=>'jerry',
            ]
        ];
        $secret = JwtAuth::generate($arr);
        Cache('jwt_secret',$secret);
        return $secret;
    }

    /**
     * 解密出原文
     * @return array
     */
    public function getJwtOriginal()
    {
        $secret = Cache::get('jwt_secret');
        $original = JwtAuth::parse($secret);
        return json($original['data']);
    }

    /**
     * 访问 http://tp8.xxx/admin/index/requestUri/id/3/name/jerry
     * @author messha1r
     * @datetime 2025-07-14
     * @param
     * @return   string   
     */
    public function requestUri ()
    {
        // index/requestUri/id/3/name/jerry
        dump($this->request->pathinfo());

        // /admin/index/requestUri/id/3/name/jerry
        dump($_SERVER['REQUEST_URI']);
        // /index.php
        dump($_SERVER['SCRIPT_NAME']);
    }

}
